﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using HandyControl.Tools.Extension;
using MiniExcelLibs;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using Zo.ExcelBox.Base;
using Zo.ExcelBox.Models;
using Zo.ExcelBox.Pages;

namespace Zo.ExcelBox.ViewModels
{
    /// <summary>
    ///  模版管理
    /// </summary>
    public partial class MapTemplateVM : ObservableObject
    {

        /// <summary>
        /// 解析的指端
        /// </summary>
        //public ObservableCollection<MapTemplate> TableDatas { get; set; } = new ObservableCollection<MapTemplate>();

        private ObservableCollection<MapTemplate> _tableDatas;
        public ObservableCollection<MapTemplate> TableDatas
        {
            get => _tableDatas;
            set => SetProperty(ref _tableDatas, value);
        }

        private string _name = string.Empty;
        /// <summary>
        /// 模版名称
        /// </summary>
        public string Name
        {
            get => _name;
            set => SetProperty(ref _name, value);
        }

        private int _pageIndex = 1;
        /// <summary>
        /// 模版名称
        /// </summary>
        public int PageIndex
        {
            get => _pageIndex;
            set => SetProperty(ref _pageIndex, value);
        }

        private int _pageSize = 20;
        /// <summary>
        /// 每页的数据量
        /// </summary>
        public int PageSize
        {
            get => _pageSize;
            set => SetProperty(ref _pageSize, value);
        }

        private int _maxPageCount = 0;
        /// <summary>
        /// 最大页数
        /// </summary>
        public int MaxPageCount
        {
            get => _maxPageCount;
            set => SetProperty(ref _maxPageCount, value);
        }

        /// <summary>
        /// 添加页面
        /// </summary>
        public IRelayCommand OnAddCommand { get; }

        /// <summary>
        /// 修改页面
        /// </summary>
        public IRelayCommand<MapTemplate> OnUpdateCommand { get; }

        /// <summary>
        /// 删除页面
        /// </summary>
        public IRelayCommand<MapTemplate> OnDeleteCommand { get; }

        /// <summary>
        /// 分页改变
        /// </summary>
        public IRelayCommand<int> OnPageChangeCommand { get; }


        /// <summary>
        /// 搜索
        /// </summary>
        public IRelayCommand OnSearchCommand { get; }

        /// <summary>
        /// 数据访问
        /// </summary>
        public static SqlSugarClient _db;

        /// <summary>
        /// 模版管理
        /// </summary>
        public MapTemplateVM()
        {
            OnAddCommand = new RelayCommand(OnAdd);
            OnUpdateCommand = new RelayCommand<MapTemplate>(OnUpdate);
            OnPageChangeCommand = new RelayCommand<int>(OnPageChange);
            OnSearchCommand = new RelayCommand(OnSearch);
            OnDeleteCommand=  new RelayCommand<MapTemplate>(OnDelete);

            _db = new DbContext().DataBase;

            LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        void LoadData(int page = 1)
        {
            var total = 0;
            var datas = _db.Queryable<MapTemplate>()
                .WhereIF(!string.IsNullOrWhiteSpace(Name), w => w.Name.Contains(Name))
                .ToPageList(page, PageSize, ref total);

            TableDatas = new ObservableCollection<MapTemplate>(datas);
            MaxPageCount = CommonHelper.CalculateTotalPages(total, PageSize);
        }


        void OnPageChange(int page)
        {
            LoadData(page);
        }

        void OnSearch()
        {
            LoadData();
        }


        void OnAdd()
        {
            OnEditPage();
        }

        void OnUpdate(MapTemplate map)
        {
            OnEditPage(map.Id);
        }

        void OnDelete(MapTemplate map) 
        {

            var result = HandyControl.Controls.MessageBox.Show("确认要删除吗？", "提示",MessageBoxButton.OKCancel, MessageBoxImage.Question);
            if (result == MessageBoxResult.OK) 
            {
                if (_db.Deleteable<MapTemplate>().Where(w => w.Id == map.Id).ExecuteCommand() > 0)
                {
                    Growl.Success("删除成功");
                    LoadData();
                }
                else 
                {
                    Growl.Error("删除失败");
                }
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        private void OnEditPage(int id = 0)
        {
            var win = new MapTemplateEditWindow(id);
            win.RequestClose += (type) =>
            {
                if (type == WindowCloseType.Succee) 
                {
                    LoadData();
                }
            };

            win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            win.Owner = System.Windows.Application.Current.MainWindow;
            win.SizeToContent = SizeToContent.Manual;
            win.ShowDialog();
        }

    }



}
